import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/my/help/help_entity.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/server/api/user_api.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';

class MyHelpLogic extends GetxController {
  List<ResourceEntity>? data;
  List<ResourceEntity> carousel = [];
  List<ResourceEntity> menus = [];
  List<QuestionType> type = [];
  List<List<QuestionEntity>>? questions;
  int _index = 0;
  final _num = (-1.0).obs;
  final ScrollController scrollController = ApmScrollController();
  final EasyRefreshController refreshController = EasyRefreshController();

  double get num {
    if (_num < 0) {
      return 0.0;
    } else if (_num > 1) {
      return 1.0;
    } else {
      return _num.value;
    }
  }

  int get index => _index;

  set index(int value) {
    _index = value;
    update();
  }

  set num(value) {
    _num.value = value;
    update();
  }

  @override
  void onReady() {
    getBack();
    getData();
    super.onReady();
  }

  Future getData() async {
    data = await OtherApi.getAppList("app_help") ?? [];
    menus =
        data?.where((element) => element.button == "menu_item").toList() ?? [];
    type = await UserApi.getQuestionType();
    questions = [];
    for (var element in type) {
      questions?.add(await UserApi.getQuestions(element.id ?? 0));
    }
    update();
  }

  Future getBack() async {
    carousel = [];
    var list = await OtherApi.getAppList("app_lbt");
    list?.forEach((element) {
      if (element.button == "help") {
        carousel.add(element);
      }
    });
    update();
  }

  doRefresh() {
    refreshController.callRefresh();
  }
}
